package com.jinlejia.erp.ui.mine.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.erp.R;
import com.jinlejia.erp.databinding.ItemCheckPictureBinding;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;


/**
 * 查看图片（Adapter）
 */

public class CheckPictureAdapter extends YfListAdapter<String> {

    private Context context;
    private ImageListener listener;

    public CheckPictureAdapter(ArrayList<String> data) {
        super(data);
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        context = parent.getContext().getApplicationContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_check_picture, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_layout, parent, false);
        TextView txt = (TextView) view.findViewById(R.id.id_tips);
        txt.setText("暂无数据");
        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindDataViewHolder(RecyclerView.ViewHolder holder, final int position) {
       ItemCheckPictureBinding binding =  DataBindingUtil.getBinding(holder.itemView);
        Picasso.with(context).load(mData.get(position)).placeholder(R.mipmap.ic_no_img).into(binding.checkPicture);
        binding.getRoot().setOnClickListener(view -> listener.OnImageClick(position));
        holder.itemView.setTag(mData.get(position));
    }

    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateFooterViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer_loading, parent, false);
        return new YfSimpleViewHolder(view);
    }

    static final class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(View itemView) {
            super(itemView);
            DataBindingUtil.bind(itemView);
        }
    }

    public interface ImageListener{
        void OnImageClick(int position);
    }

    public void OnImageListener(ImageListener listener){
        this.listener = listener;
    }
}
